package com.calf.module.jpush;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.utils.StringUtils;

/**
 * @ClassName JPushAllUtil
 * @Description 极光推送工具类
 * @author cx
 * @Date 2017年3月13日 下午8:42:17
 * @version 1.0.0
 */
public class JPushUtil {
    private final static String appKey = "69e52e0301b93dd98e3d7643";
    private final static String masterSecret = "7c817f24046c2c1575ec3f6d";
//    private final static String ALERT = "您有一条新的消息!小牛速贷";
//    private final static String SENDNO = "";
    /**
     * 测试方法
     */
    public static void main(String[] args) {
        //发送通知
//        jSend_android_notification("17060521091569b06822","【小牛速贷】您有一条消息！","测试","https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/", "1");

    }
    /**
     * 发送通知
     * @param alias 设备标识
     * @param alert 推送内容
     */
    public static void jSend_notification(String alias, String alert, String title, String url, String target){
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
//        PushPayload payload = send_N(registrationId, alert);
        PushPayload payload = null;
        if (StringUtils.isEmpty(url)){
            payload = buildPage(alias, alert, title, url, target);
        }else{
            payload = buildH5(alias, alert, title, url, target);
        }
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result);

        } catch (APIConnectionException e) {
            System.out.println(e);
        } catch (APIRequestException e) {
            System.out.println(e);
            System.out.println("Error response from JPush server. Should review and fix it. "+ e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }

    /**
     *
     * @Description 给所有平台所用设备推送
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert(String alert) {
        return PushPayload.alertAll(alert);
    }

    /**
     *
     * @Description 针对所有用户发送根据别名单个发送
     * @param alias 用户别名
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String alias, String alert) {
        String [] strs = alias.split(",");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(strs))
                .setNotification(Notification.alert(alert))
                .build();
    }

    /**
     *
     * @Description 针对所有用户发送根据不同情况发送通知
     * @param alias 用户别名
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert_(String alias, String alert, String title, String url, String target) {
        String [] strs = alias.split(",");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(strs))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(alert)
                                .addExtra("from", "JPush")
                                .addExtra("push_url", url)
                                .addExtra("push_title", title)
                                .addExtra("target", target)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .addExtra("push_url", url)
                                .addExtra("push_title", title)
                                .addExtra("target", target)
                                .build())
                        .build())
                  .setMessage(Message.content(alert))
                  .setOptions(Options.newBuilder()
                          .setApnsProduction(true)
                          .build())
                .build();
    }

    /**
     *
     * @Description 针对所有用户发送给多个用户推送
     * @param aliass 用户别名集合
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String aliass, String alert) {
        String [] strs = aliass.split(",");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(strs))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(alert)
                        .addExtra("from", "JPush")
                        .addExtra("url", "http://www.baidu.com")
                        .build())
                .build();
    }


    public static PushPayload buildH5(String alias, String alert, String title, String url, String target) {
        String [] strs = alias.split(",");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(strs))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(alert)
                                .addExtra("from", "JPush")
                                .addExtra("push_url", url)
                                .addExtra("push_title", title)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .addExtra("target", target)
                                .addExtra("push_url", url)
                                .addExtra("push_title", title)
                                .build())
                        .build())
                  .setMessage(Message.content(alert))
                  .setOptions(Options.newBuilder()
                          .setApnsProduction(true)
                          .build())
                .build();
    }

    public static PushPayload buildPage(String alias, String alert, String title, String url, String target) {
        String [] strs = alias.split(",");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(strs))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(alert)
                                .addExtra("target", "100")
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(5)
                                .setSound("happy")
                                .build())
                        .build())
                  .setMessage(Message.content(alert))
                  .setOptions(Options.newBuilder()
                          .setApnsProduction(true)
                          .build())
                .build();
    }
}
